using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;


namespace Mall.Api.Controllers
{
    /// <summary>
    /// 首页
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("home")]
    public class CategoryListingsController : ControllerBase
    {
        private IRepository<Class> _class;
        private IRepository<Brand> _brand;
        private IRepository<Commodity> _commodity;
        private IRepository<Supplier> _supplier;
        // private IRepository<Commodity> _commodity;

        private readonly IConfiguration _configuration;

        public CategoryListingsController(IConfiguration configuration, IRepository<Class> classes, IRepository<Brand> brand,
        IRepository<Commodity> commodity, IRepository<Supplier> supplier)
        {
            _configuration = configuration;
            _class = classes;
            _brand = brand;
            _commodity = commodity;
            _supplier = supplier;


        }

        /// <summary>
        /// 获取首页分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic Get()
        {

            // join classes in _class.Table on commodity.ClassId equals classes.Id
            var dataList = (from commodity in _commodity.Table
                            join supplier in _supplier.Table on commodity.SupplierId equals supplier.Id
                            join brand in _brand.Table on supplier.BrandId equals brand.Id
                            orderby supplier.Priority
                            where supplier.Priority != 0
                            select new
                            {
                                ClassId = commodity.ClassId,
                                BrandId = brand.Id,
                                BrandName = brand.BrandName,
                                commodityId = commodity.Id,
                                ProductName = commodity.ProductName
                            }).ToList();


            var classEt = (from cs in dataList
                           group cs by new { cs.ClassId }
            into s
                           select new { ClassId = s.Key.ClassId, Count = s.Count() }).ToList();


            var brandEt = (from bd in dataList
                           group bd by new { bd.BrandName, bd.BrandId }
            into s
                           select new { BrandName = s.Key.BrandName, BrandId = s.Key.BrandId, Count = s.Count() }).ToList();


            var classTd = _class.Table.ToList();

            var homeList = new List<HomeList>();
            foreach (var oneClass in classEt)
            {
                var homeBrands = new List<HomeBrand>();


                foreach (var oneBrand in brandEt)
                {

                    var homeCommitys = new List<HomeCommity>();
                    foreach (var item in dataList)
                    {
                        if (item.BrandId == oneBrand.BrandId && item.ClassId == oneClass.ClassId)
                        {
                            homeCommitys.Add(new HomeCommity
                            {
                                CommodityId = item.commodityId,
                                ProductName = item.ProductName
                            });
                        }


                    }
                    if (homeCommitys.Count() > 0)
                    {
                        homeBrands.Add(new HomeBrand
                        {
                            BrandId = oneBrand.BrandId,
                            BrandName = oneBrand.BrandName,
                            HomeCommities = homeCommitys
                        });
                    }

                }


                var className = "";
                foreach (var item in classTd)
                {
                    if (item.Id == oneClass.ClassId)
                    {
                        className = item.ClassName;
                    }
                }

                homeList.Add(new HomeList
                {
                    ClassId = oneClass.ClassId,
                    ClassName = className,
                    HomeBrands = homeBrands
                });
            }




            var res = new
            {
                Code = 650,
                Data = homeList,
                Msg = "查找成功！"
            };
            return JsonHelper.Serialize(res);



        }













    }
}